import wepy from 'wepy'

export default class extends wepy.mixin {
  data = {
    address: '',
    carts: [],
    isLogin: true
  }

  methods = {
    async addAddress() {
      const address = await wepy.chooseAddress()

      if (address.errMsg !== 'chooseAddress:ok') {
        return 
      }

      this.address = address
      wepy.setStorageSync('address', address)
      this.$apply()
    },
    async loginBuy(userInfo){
      if(userInfo.detail.errMsg !== 'getUserInfo:ok') {
        return wepy.baseToast('获取用户信息失败')
      }

      let info = userInfo.detail

      let loginInfo = await wepy.login()

      if(loginInfo.errMsg !== 'login:ok') {
        return wepy.baseToast('登录失败失败')
      }

      let wxloginParams = {
        code: loginInfo.code,
        encryptedData: info.encryptedData,
        iv: info.iv,
        rawData : info.rawData,
        signature: info.signature
      }

      let {data: wxloginInfo} = await wepy.post('/users/wxlogin', wxloginParams)

      if(wxloginInfo.meta.status !== 200) {
        return wepy.baseToast('请求接口登录失败')
      }

      let token = wxloginInfo.message.token
      this.isLogin = false
      wepy.setStorageSync('token', token)
      
      this.$apply()
    
    },
    async onSubmit() {
      if(!this.address) {
        return wepy.baseToast('请选择收货地址')
      }
      if(this.allPrice <= 0) {
        return wepy.baseToast('订单金额不能为 0')
      }

      let params = {
        order_price: '0.01',
        consignee_addr: this.addressInfo,
        goods: this.carts.map(item => {
          return {
            goods_id: item.id,
            goods_number: item.count,
            goods_price: item.price,
          }
        })
      }

      let {data: createData} = await wepy.post('/my/orders/create', params)

      if(createData.meta.status !== 200) {
        return wepy.baseToast('创建订单失败')
      }

      let {data: presetData} = await wepy.post('/my/orders/req_unifiedorder', {order_number: createData.message.order_number})

      if(presetData.meta.status !== 200) {
        return wepy.baseToast('生成预支付订单失败')
      }

      let payData = await wepy.requestPayment(presetData.message.pay).catch(err => err)

      if (payData.errMsg !== 'requestPayment:ok') {
        return wepy.baseToast('您取消了支付')
      }
 
      let {data: chkData} = await wepy.post('/my/orders/chkOrder', {order_number: createData.message.order_number})

      if(chkData.meta.status !== 200) {
        return wepy.baseToast('支付失败')
      }
      
      wepy.navigateTo({
        url: '/pages/orderList'
      })
    }
  }

  onLoad() {
    this.address = wepy.getStorageSync('address') || ''
    this.carts = this.$parent.globalData.carts.filter(item => item.isCheck)
  }

  computed = {
    addressInfo() { 
      if(!this.address) {
        return 
      }
      let {provinceName, cityName, countyName, detailInfo} = this.address

      return provinceName + cityName + countyName + detailInfo
    },

    allPrice() {
      var num = 0
      this.carts.forEach(item=>{
        num += item.count * item.price
      })

      return num * 100
    }
  }
}
